package com.icss.ah.platformActivity.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.SyncSelectTree;

@Service
@Transactional
public class ActivityProductService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	/**
	 * 获取平台活动参与商品
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProductList(Map<String, String> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = sqlSessionTemplate.selectList(
					"ActivityProductBean.getProductList", map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|getProductList", e);
		}
		return list;
	}

	/**
	 * 获取平台活动参与商品总数
	 * @param map
	 * @return
	 */
	public Long getProductTotal(Map<String, String> map) {
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("ActivityProductBean.getProductTotal",map);
			infolog.info("ActivityProductService|getProductTotal|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|getProductTotal", e);
		}
		return count;
	}
	
	/**
	 * 获取商品分类树
	 */
	public List<SyncSelectTree> getTree(SyncSelectTree selectTree) {
		String id = selectTree.getId();//id 异步加载展开树时传到后台的id
		String ifAll = selectTree.getIfAll();//是否一次性展示所有数据，1是 0否
		List<SyncSelectTree> list = new ArrayList<SyncSelectTree>();
		try {
			if (ifAll != null && !"".equals(ifAll)) {
				if ("0".equals(ifAll)) {//展开树时加载数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("ActivityProductBean.getTree",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("ActivityProductBean.getTreeChild",selectTree);
					}
				}else{//一次加载所有数据
					if ("".equals(id)) {//如果查询id没有值，表示查询根节点
						list = sqlSessionTemplate.selectList("ActivityProductBean.getTreeAll",selectTree);
					}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
						list = sqlSessionTemplate.selectList("ActivityProductBean.getTreeChild",selectTree);
					}
				}
			}
			infolog.info("ActivityProductService|getTree|list:"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|getTree", e);
		}
		return list == null ? new ArrayList<SyncSelectTree>() : list;
	}

	/**
	 * 根据商品分类code获取 活动未添加商品
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getNoAddProduct(Map<String, String> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = sqlSessionTemplate.selectList(
					"ActivityProductBean.getNoAddProduct", map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|getNoAddProduct", e);
		}
		return list;
	}

	/**
	 * 批量保存商品
	 * @param list
	 * @return
	 */
	public Boolean saveProduct(List<Map<String,String>> list) {
		boolean result=false;
		try {
			sqlSessionTemplate.insert("ActivityProductBean.saveProduct", list);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|saveProduct", e);
		}
		return result;
	}

	/**
	 * 删除商品
	 * @param map
	 * @return
	 */
	public Boolean deleteProduct(Map<String, String> map) {
		boolean result=false;
		try {
			sqlSessionTemplate.delete("ActivityProductBean.deleteProduct", map);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|deleteProduct", e);
		}
		return result;
	}

	/**
	 * 批量删除商品
	 * @param activityProUuidsList
	 * @return
	 */
	public Boolean deleteProducts(List<String> activityProUuidsList) {
		boolean result=false;
		try {
			sqlSessionTemplate.delete("ActivityProductBean.deleteProducts", activityProUuidsList);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|deleteProducts", e);
		}
		return result;
	}

	/**
	 * 湖区偶未参加商品总数
	 * @param map
	 * @return
	 */
	public Long getNoAddProductTotal(Map<String, String> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Long count = 0L;
		try {
			list = sqlSessionTemplate.selectList("ActivityProductBean.getNoAddProductTotal",map);
			count=(long) list.size();
			infolog.info("ActivityProductService|getNoAddProductTotal|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ActivityProductService|getNoAddProductTotal", e);
		}
		return count;
	}
	
}
